﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Utils;
using QFlow.Data;

namespace QFlow.Windows.Reports
{
    public partial class AccountingLogsReportViewer : BaseForm
    {
        public AccountingLogsReportViewer(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void AccountingLogsReportViewer_Load(object sender, EventArgs e)
        {
            cmbUsers.DataSource = this.Context.Users.ToArray();
            cmbUsers.SelectedIndex = -1;

            cmbAccountingEvents.DataSource = this.Context.AccountingEvents;
            cmbAccountingEvents.SelectedIndex = -1;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int eventId = cmbAccountingEvents.SelectedIndex == -1 ? 0 : (int)cmbAccountingEvents.SelectedValue;
            int userId = (cmbUsers.SelectedIndex == -1 ? 0 : (int)cmbUsers.SelectedValue);

            DateTime fromDate = dtFrom.Value.Date;
            DateTime toDate = dtTo.Value.Date.AddHours(24);

            var result = from log in Context.AccountingLogs
                         where log.TimeStamp >= fromDate
                &&
                log.TimeStamp <= toDate
                &&
                (eventId == 0 ? true : log.AccontingEventID == eventId)
                &&
                (userId == 0 ? true : log.UserID == userId)

                         select new AccountingLogsReportResult()
                         {
                             CashirCredit = log.CashirCredit,
                             SafeCredit = log.SafeCredit,
                             PurchaseCredit = log.PurchaseCredit,
                             PurchaseReturn = log.PurchaseReturn,
                             SaleCredit = log.SalesCredit,
                             SaleReturn = log.SalesReturn,
                             User = log.User.Username,
                             AccountingEvent = log.AccountingEvent.AccountingEvent1,
                             Date = log.TimeStamp

                         };

            AccountingLogsReportResultBindingSource.DataSource = result.ToArray();

            List<Microsoft.Reporting.WinForms.ReportParameter> repeortParams = new List<Microsoft.Reporting.WinForms.ReportParameter>();
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("FromDate", fromDate.ToString("dd-MM-yyyy")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("ToDate", toDate.ToString("dd-MM-yyyy")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("Event", (eventId != 0 ? cmbAccountingEvents.Text : "All")));
            repeortParams.Add(new Microsoft.Reporting.WinForms.ReportParameter("User", (userId != 0 ? cmbUsers.Text : "All")));

            reportViewer1.LocalReport.SetParameters(repeortParams);

            reportViewer1.RefreshReport();
        }
    }

    public class AccountingLogsReportResult
    {
        public double CashirCredit { get; set; }
        public double SafeCredit { get; set; }
        public double PurchaseCredit { get; set; }
        public double PurchaseReturn { get; set; }
        public double SaleCredit { get; set; }
        public double SaleReturn { get; set; }
        public string User { get; set; }
        public string AccountingEvent { get; set; }
        public DateTime Date { get; set; }
    }
}
